package com.jingdianjichi.subject.domin.handler.subject;

import com.jingdianjichi.subject.common.enums.SubjectInfoTypeEnum;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 题目类型工厂
 * @author 86156
 * @version 1.0
 * @date 2024-10-21 14:03
 * @知识 实现InitializingBean接口 在Bean初始化之后执行afterPropertiesSet方法
 */
@Component
public class SubjectTypeHandlerFactory implements InitializingBean {
    @Resource
    private List<SubjectTypeHandler> subjectTypeHandlers;

    private Map<SubjectInfoTypeEnum,SubjectTypeHandler> handlerMap = new HashMap<>();

    public SubjectTypeHandler getHandler(Integer handlerType){
        SubjectInfoTypeEnum typeEnum = SubjectInfoTypeEnum.getByCode(handlerType);
        return handlerMap.get(typeEnum);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        for (SubjectTypeHandler subjectTypeHandler : subjectTypeHandlers) {
            handlerMap.put(subjectTypeHandler.getSubjectType(),subjectTypeHandler);
        }
    }
}
